home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / utils / map-ynp.el < prev    next >
Encoding:
Text File  |  1995-04-17  |  5.3 KB  |  157 lines

  1. ;;; map-ynp.el --- General-purpose boolean question-asker
  2.  
  3. ;; Keywords: extensions
  4.  
  5. ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  6. ;;; Written by Roland McGrath.
  7. ;;;
  8. ;;; This program is free software; you can redistribute it and/or modify
  9. ;;; it under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 1, or (at your option)
  11. ;;; any later version.
  12. ;;;
  13. ;;; This program is distributed in the hope that it will be useful,
  14. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; A copy of the GNU General Public License can be obtained from this
  19. ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
  20. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  21. ;;; 02139, USA.
  22. ;;;
  23. ;;; map-y-or-n-p is a general-purpose question-asking function.
  24. ;;; It asks a series of y/n questions (a la y-or-n-p), and decides to
  25. ;;; applies an action to each element of a list based on the answer.
  26. ;;; The nice thing is that you also get some other possible answers
  27. ;;; to use, reminiscent of query-replace: ! to answer y to all remaining
  28. ;;; questions; ESC or q to answer n to all remaining questions; . to answer
  29. ;;; y once and then n for the remainder; and you can get help with C-h.
  30.  
  31. (defun map-y-or-n-p-help (object objects action)
  32.   (format "Type SPC or `y' to %s the current %s;
  33. DEL or `n' to skip the current %s;
  34. ! to %s all remaining %s;
  35. ESC or `q' to exit;
  36. or . (period) to %s the current %s and exit."
  37.       action object object action objects action object))
  38.  
  39. ;;;###autoload
  40. (defun map-y-or-n-p (prompter actor list &optional help)
  41.   "Ask a series of boolean questions.
  42. Takes args PROMPTER ACTOR LIST, and optional arg HELP.
  43.  
  44. LIST is a list of objects, or a function of no arguments to return the next
  45. object or nil.
  46.  
  47. If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\).  If not
  48. a string, PROMPTER is a function of one arg (an object from LIST), which
  49. returns a string to be used as the prompt for that object.  If the return
  50. value is not a string, it is eval'd to get the answer; it may be nil to
  51. ignore the object, t to act on the object without asking the user, or a
  52. form to do a more complex prompt.
  53.  
  54.  
  55. ACTOR is a function of one arg (an object from LIST),
  56. which gets called with each object that the user answers `yes' for.
  57.  
  58. If HELP is given, it is a list (OBJECT OBJECTS ACTION),
  59. where OBJECT is a string giving the singular noun for an elt of LIST;
  60. OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive
  61. verb describing ACTOR.  The default is \(\"object\" \"objects\" \"act on\"\).
  62.  
  63. At the prompts, the user may enter y, Y, or SPC to act on that object;
  64. n, N, or DEL to skip that object; ! to act on all following objects;
  65. ESC or q to exit (skip all following objects); . (period) to act on the
  66. current object and then exit; or \\[help-command] to get help.
  67.  
  68. Returns the number of actions taken."
  69.   (let* ((old-help-form help-form)
  70.      (help-form (cons 'map-y-or-n-p-help
  71.               (or help '("object" "objects" "act on"))))
  72.      (actions 0)
  73.      prompt
  74.      char
  75.      elt
  76.      (next (if (or (symbolp list)
  77.                (subrp list)
  78.                (compiled-function-p list)
  79.                (and (consp list)
  80.                 (eq (car list) 'lambda)))
  81.            (function (lambda ()
  82.                    (setq elt (funcall list))))
  83.          (function (lambda ()
  84.                  (if list
  85.                  (progn
  86.                    (setq elt (car list)
  87.                      list (cdr list))
  88.                    t)
  89.                    nil))))))
  90.     (if (stringp prompter)
  91.     (setq prompter (` (lambda (object)
  92.                 (format (, prompter) object)))))
  93.     (while (funcall next)
  94.       (setq prompt (funcall prompter elt))
  95.       (if (stringp prompt)
  96.       (progn
  97.         ;; Prompt the user about this object.
  98.         (let ((cursor-in-echo-area t))
  99.           (message "%s(y, n, ! ., q, or %s)"
  100.                prompt (key-description (char-to-string help-char)))
  101.           (setq char (read-char)))
  102.         (cond ((or (= ?q char)
  103.                (= ?\e char))
  104.            (setq next (function (lambda () nil))))
  105.           ((or (= ?y char)
  106.                (= ?Y char)
  107.                (= ?  char))
  108.            ;; Act on the object.
  109.            (let ((help-form old-help-form))
  110.              (funcall actor elt))
  111.            (setq actions (1+ actions)))
  112.           ((or (= ?n char)
  113.                (= ?N char)
  114.                (= ?\^? char))
  115.            ;; Skip the object.
  116.            )
  117.           ((= ?. char)
  118.            ;; Act on the object and then exit.
  119.            (funcall actor elt)
  120.            (setq actions (1+ actions)
  121.              next (function (lambda () nil))))
  122.           ((= ?! char)
  123.            ;; Act on this and all following objects.
  124.            (if (eval (funcall prompter elt))
  125.                (progn
  126.              (funcall actor elt)
  127.              (setq actions (1+ actions))))
  128.            (while (funcall next)
  129.              (if (eval (funcall prompter elt))
  130.              (progn
  131.                (funcall actor elt)
  132.                (setq actions (1+ actions))))))
  133.           ((= ?? char)
  134.            (setq unread-command-char help-char)
  135.            (setq next (` (lambda ()
  136.                    (setq next '(, next))
  137.                    '(, elt)))))
  138.           (t
  139.            ;; Random char.
  140.            (message "Type %s for help."
  141.                 (key-description (char-to-string help-char)))
  142.            (beep)
  143.            (sit-for 1)
  144.            (setq next (` (lambda ()
  145.                    (setq next '(, next))
  146.                    '(, elt)))))))
  147.     (if (eval prompt)
  148.         (progn
  149.           (funcall actor elt)
  150.           (setq actions (1+ actions))))))
  151.     ;; Clear the last prompt from the minibuffer.
  152.     (message "")
  153.     ;; Return the number of actions that were taken.
  154.     actions))
  155.  
  156. ;;; map-ynp.el ends here
  157.